package cn.cqnu.domin.vo;


import cn.cqnu.global.SystemReturnEnum;
import com.fasterxml.jackson.annotation.JsonInclude;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.io.Serializable;

/**
 * 响应结果
 *
 * @author Wei
 * @date 2023/12/30
 */
@Data
@AllArgsConstructor
@NoArgsConstructor
// 这个注解可以在json化的过程中跳过为空的字段
@JsonInclude(JsonInclude.Include.NON_NULL)
public class ResponseResult<T> implements Serializable {

    private Integer code;

    private String message;

    private T data;


//    lombok生成全参构造

    //    无数据体的构造
    ResponseResult(Integer code, String message) {
        this.code = code;
        this.message = message;
    }


    /**
     * 返回错误信息, 无数据
     *
     * @param returnEnum 返回枚举
     * @return {@link ResponseResult}
     */
    public static ResponseResult error(SystemReturnEnum returnEnum) {
        return new ResponseResult<>(returnEnum.getCode(), returnEnum.getMessage());
    }

    public static ResponseResult error(Integer code, String message) {
        return new ResponseResult<>(code, message);
    }


    /**
     * ok响应
     *
     * @return {@link ResponseResult}
     */
    public static ResponseResult ok(){
        return new ResponseResult(SystemReturnEnum.OK.getCode(), SystemReturnEnum.OK.getMessage());
    }

    /**
     * ok结果响应
     *
     * @param data 数据
     * @return {@link ResponseResult}
     */
    public static ResponseResult ok(Object data) {
        return new ResponseResult(SystemReturnEnum.OK.getCode(), SystemReturnEnum.OK.getMessage(), data);
    }

}
